//
//  AliMNSHttp.m
//  AlibabaCloudMNSiOS
//
//  Created by David Chen on 28/03/2017.
//  Copyright © 2017 AliCloud. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "AliMNSHttp.h"
#import "AliMNSException.h"

@implementation AliMNSHttpRequestInternal
@synthesize method;
@synthesize uri;
@synthesize header;
@synthesize data;
@end

@implementation AliMNSHttpResponseInternal

@synthesize status;
@synthesize header;
@synthesize data;
@synthesize error;
@end

@implementation AliMNSHttp
@synthesize host;
@synthesize connectionTimeout;
@synthesize keepAlive;
@synthesize requestSize;
@synthesize responseSize;
@synthesize isHttps;

/*
 send synchronous rquest
*/
-(void) sendSyncRequest:(AliMNSHttpRequestInternal*)httpRequest
       withResponse:(AliMNSHttpResponseInternal*)httpResponse
{

    NSString* urlStr = [NSString stringWithFormat:@"%@%@", host, httpRequest.uri];
    NSURL * url = [[NSURL alloc] initWithString:urlStr];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setAllHTTPHeaderFields: httpRequest.header];
    [request setHTTPBody:httpRequest.data];
    [request setHTTPMethod:httpRequest.method];

    NSURLResponse* response = nil;
    NSError* error = nil;
    httpResponse.data = [NSURLConnection sendSynchronousRequest:request returningResponse: &response error:&error];

    httpResponse.error = error;
    NSHTTPURLResponse *hResponse = (NSHTTPURLResponse *)response;
    httpResponse.status = [NSString stringWithFormat: @"%ld", (long)[hResponse statusCode]];
    httpResponse.header = [hResponse allHeaderFields];
}

/*
 send asynchronous rquest.
 */

-(void) sendAsyncRequest:(AliMNSHttpRequestInternal*) httpRequest
            withResponse:(AliMNSHttpResponseInternal*)httpResponse
             withHandler:(void (^)(AliMNSHttpResponseInternal* internalResponse)) responseHandler
{
    NSString* urlStr = [NSString stringWithFormat:@"%@%@", host, httpRequest.uri];
    NSURL * url = [[NSURL alloc] initWithString:urlStr];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

    [request setAllHTTPHeaderFields: httpRequest.header];
    [request setHTTPBody:httpRequest.data];
    [request setHTTPMethod:httpRequest.method];

    NSURLSession * session = [NSURLSession sharedSession];
    NSURLSessionTask* task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable result, NSURLResponse * _Nullable response, NSError * _Nullable error) {

        httpResponse.data = result;
        NSHTTPURLResponse *hResponse = (NSHTTPURLResponse *)response;
        httpResponse.status = [NSString stringWithFormat: @"%ld", (long)[hResponse statusCode]];
        httpResponse.header = [hResponse allHeaderFields];
        httpResponse.error = error;

        //NSLog(@"DebugInfo:status=%@,header=%@, errorStr=%@, data=%@", httpResponse.status, httpResponse.header, [httpResponse.error localizedDescription], [[NSString alloc] initWithData: httpResponse.data encoding:NSASCIIStringEncoding]);

        responseHandler(httpResponse);
    }];

    [task resume];
}

@end
